import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../lib_baseview.dart';

class AvatarImage extends StatelessWidget {
  final String? url;
  final double width;
  final double height;
  final PlaceholderWidgetBuilder? placeholder;
  final LoadingErrorWidgetBuilder? errorWidget;
  final BoxBorder? border;
  final Gradient? borderGradient;
  final double borderRadius;
  final String? topRightBadgeTitle;
  final Widget? topRightBadge;
  final Widget? bottomRightBadge;
  final BoxFit? fit;
  BoxShape shape;
  final bool isLocalAvatar;

  AvatarImage(
      {super.key,
      required this.url,
      required this.width,
      required this.height,
      this.borderRadius = 0,
      this.borderGradient,
      this.placeholder,
      this.border,
      this.errorWidget,
      this.topRightBadge,
      this.topRightBadgeTitle,
      this.bottomRightBadge,
      this.fit,
      this.shape = BoxShape.circle,
      this.isLocalAvatar = false});

  @override
  Widget build(BuildContext context) {
    if (width != height && shape != BoxShape.rectangle) {
      shape = BoxShape.rectangle;
    }

    Widget iconChild = image(
      url ?? "",
      width: width,
      height: height,
      fit: fit ?? BoxFit.cover,
      placeholder: placeholder ??
          (context, url) => Image.asset(
                'assets/images/contact_avatar_default.png',
                width: width,
                height: height,
              ),
      errorWidget: errorWidget ??
          (context, url, error) {
            if (placeholder != null) {
              return placeholder!(context, url);
            }
            return Image.asset(
              'assets/images/contact_avatar_default.png',
              width: width,
              height: height,
            );
          },
    );
    if (shape == BoxShape.circle) {
      iconChild = ClipOval(child: iconChild);
    } else {
      iconChild = ClipRRect(
          borderRadius: BorderRadius.circular(borderRadius), child: iconChild);
    }

    return Stack(
      children: [
        Container(
            width: width,
            height: height,
            clipBehavior: Clip.antiAlias,
            decoration: BoxDecoration(
                border: border,
                borderRadius: shape == BoxShape.circle
                    ? null
                    : BorderRadius.circular(borderRadius),
                gradient: borderGradient,
                shape: shape),
            child: iconChild),
        Positioned(
          top: 0,
          right: 0,
          child: _buildTopRightBadge(),
        ),
        Positioned(
          bottom: 0,
          right: 0,
          child: bottomRightBadge ?? const SizedBox.shrink(),
        ),
      ],
    );
  }

  Widget _buildTopRightBadge() {
    if (topRightBadge != null) {
      return topRightBadge!;
    }
    if (topRightBadgeTitle == null || topRightBadgeTitle!.isEmpty) {
      return const SizedBox.shrink();
    }
    var titleText = topRightBadgeTitle ?? "";
    return Container(
      width: 15,
      height: 15,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: [Color(0xFFFF7A7A), Color(0xFFFA4747)],
        ),
        shape: BoxShape.circle,
      ),
      child: Text(
        titleText,
        style: TextStyle(
            fontSize: titleText.length <= 1 ? 11 : 9, color: Colors.white),
      ),
    );
  }
}
